package com.shkco.xbjcc.web.controller;

import com.shkco.xbjcc.domain.UserIntegral;
import com.shkco.xbjcc.service.IUserInviteCodeService;
import com.shkco.xbjcc.domain.UserInviteCode;
import com.shkco.xbjcc.query.UserInviteCodeQuery;
import com.shkco.result.JSONResult;
import com.shkco.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userInviteCode")
public class UserInviteCodeController {

    @Autowired
    public IUserInviteCodeService userInviteCodeService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody UserInviteCode userInviteCode){
        if(userInviteCode.getId()!=null){
            userInviteCodeService.updateById(userInviteCode);
        }else{
            userInviteCodeService.insert(userInviteCode);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        userInviteCodeService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userInviteCodeService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(userInviteCodeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserInviteCodeQuery query){
        Page<UserInviteCode> page = new Page<UserInviteCode>(query.getPage(),query.getRows());
        page = userInviteCodeService.selectPage(page);
        return JSONResult.success(new PageList<UserInviteCode>(page.getTotal(),page.getRecords()));
    }


    @RequestMapping(value = "/getInviteCode/{username}",method = RequestMethod.GET)
    public UserInviteCode getInviteCode(@PathVariable("username")String username){
        return userInviteCodeService.getInviteCode(username);
    }
}
